<Project>
  <PropertyGroup>
    <!-- OutDir is not set early enough to set this property in .props file. -->
    <BundleDir>$([MSBuild]::NormalizeDirectory('$(OutDir)', 'AppBundle'))</BundleDir>
    <PublishDir Condition="'$(UseAppBundleRootForBuildingTests)' == 'true' and '$(IgnoreForCI)' != 'true' and '$(IsFunctionalTest)' != 'true'">$(AppBundleRoot)tests\$(AssemblyName)</PublishDir>
    <PublishDir Condition="'$(UseAppBundleRootForBuildingTests)' == 'true' and '$(IgnoreForCI)' != 'true' and '$(IsFunctionalTest)' == 'true'">$(AppBundleRoot)runonly\$(AssemblyName)</PublishDir>
    <BundleDir Condition="'$(UseAppBundleRootForBuildingTests)' == 'true' and '$(IgnoreForCI)' != 'true'">$([MSBuild]::NormalizeDirectory('$(PublishDir)', 'AppBundle'))</BundleDir>
    <RunScriptOutputPath>$([MSBuild]::NormalizePath('$(BundleDir)', '$(RunScriptOutputName)'))</RunScriptOutputPath>

    <PublishingTestsRun>true</PublishingTestsRun>
    <PublishTestAsSelfContainedDependsOn Condition="'$(PublishTestAsSelfContainedDependsOn)' == ''">Publish</PublishTestAsSelfContainedDependsOn>
    <PublishTestAsSelfContainedAfterTargets Condition="'$(PublishTestAsSelfContainedAfterTargets)' == ''">Build</PublishTestAsSelfContainedAfterTargets>

    <SkipWorkloadsTestingTargetsImport Condition="'$(SkipWorkloadsTestingTargetsImport)' == ''">true</SkipWorkloadsTestingTargetsImport>
  </PropertyGroup>

  <PropertyGroup Condition="'$(TargetOS)' == 'ios' or '$(TargetOS)' == 'tvos'">
    <RunAOTCompilation Condition="'$(RuntimeFlavor)' == 'Mono' ">true</RunAOTCompilation>
    <UseNativeAOTRuntime Condition="'$(RuntimeFlavor)' == 'CoreCLR'">true</UseNativeAOTRuntime>
  </PropertyGroup>

  <PropertyGroup>
    <DotnetPgoToolDir>$([MSBuild]::NormalizeDirectory('$(CoreCLRToolPath)', 'dotnet-pgo'))</DotnetPgoToolDir>
    <DotnetPgoToolPath>$([MSBuild]::NormalizePath('$(DotnetPgoToolDir)', 'dotnet-pgo'))</DotnetPgoToolPath>
  </PropertyGroup>

  <PropertyGroup Condition="'$(EnableAggressiveTrimming)' == 'true' or '$(EnableSoftTrimming)' == 'true'">
    <PublishTrimmed>true</PublishTrimmed>
    <!-- Suppress trimming warnings as these are tests -->
    <SuppressTrimAnalysisWarnings>true</SuppressTrimAnalysisWarnings>
    <NoWarn>$(NoWarn);IL2103;IL2105;IL2025;IL2111</NoWarn>
    <!-- Warns about missing Xamarin.iOS.dll -->
    <NoWarn Condition="'$(EnableSoftTrimming)' == 'true'">$(NoWarn);IL2035</NoWarn>
    <!-- https://github.com/dotnet/sdk/issues/18581 tracks needing to set 2 properties to disable warnings. -->
    <EnableTrimAnalyzer>false</EnableTrimAnalyzer>

    <!-- Reduce library test app size by trimming framework library features  -->
    <DebuggerSupport Condition="'$(DebuggerSupport)' == '' and '$(Configuration)' != 'Debug'">false</DebuggerSupport>
    <EnableUnsafeUTF7Encoding Condition="'$(EnableUnsafeUTF7Encoding)' == ''">false</EnableUnsafeUTF7Encoding>
    <EventSourceSupport Condition="'$(EventSourceSupport)' == ''">false</EventSourceSupport>
    <HttpActivityPropagationSupport Condition="'$(HttpActivityPropagationSupport)' == ''">false</HttpActivityPropagationSupport>
    <UseSystemResourceKeys Condition="'$(UseSystemResourceKeys)' == ''">false</UseSystemResourceKeys>
    <UseNativeHttpHandler Condition="'$(UseNativeHttpHandler)' == ''">false</UseNativeHttpHandler>
  </PropertyGroup>

  <!-- When trimming non-exe projects, root the whole intermediate assembly.
       The SDK settings root only the entry point by default. These targets are used not only for standard
       console apps, but also for test projects without an entry point. -->
  <Target Name="RootEntireIntermediateAssembly" AfterTargets="PrepareForILLink" Condition="'$(OutputType)' != 'Exe'">
    <ItemGroup>
      <TrimmerRootAssembly Condition="'%(Identity)' == '@(IntermediateAssembly)'">
        <RootMode>all</RootMode>
      </TrimmerRootAssembly>
    </ItemGroup>
  </Target>

  <PropertyGroup>
    <AdditionalXHarnessArguments Condition="'$(ExpectedExitCode)' != ''">$(AdditionalXHarnessArguments) --expected-exit-code $(ExpectedExitCode)</AdditionalXHarnessArguments>
  </PropertyGroup>

  <PropertyGroup>
    <DiagnosticPorts Condition="'$(DiagnosticStartupMode)' != ''">$(DiagnosticPorts),$(DiagnosticStartupMode)</DiagnosticPorts>
  </PropertyGroup>

  <PropertyGroup Condition="'$(TargetOS)' == 'android'">
    <!-- The -arg flag for xharness passes the arguments along to the instrumentation app -->
    <AdditionalXHarnessArguments Condition="'$(XUnitMethodName)' != ''">$(AdditionalXHarnessArguments) --arg=-m=$(XUnitMethodName)</AdditionalXHarnessArguments>
    <AdditionalXHarnessArguments Condition="'$(XUnitClassName)' != ''">$(AdditionalXHarnessArguments) --arg=-c=$(XUnitClassName)</AdditionalXHarnessArguments>
  </PropertyGroup>

  <PropertyGroup Condition="'$(TargetOS)' == 'maccatalyst' or '$(TargetOS)' == 'ios' or '$(TargetOS)' == 'iossimulator' or '$(TargetOS)' == 'tvos' or '$(TargetOS)' == 'tvossimulator'">
    <!-- Pass the -m or -c flag along to the app bundle, note that due to the double hyphen this needs to be the last argument -->
    <AdditionalXHarnessArguments Condition="'$(XUnitMethodName)' != ''">$(AdditionalXHarnessArguments) -- -m=$(XUnitMethodName)</AdditionalXHarnessArguments>
    <AdditionalXHarnessArguments Condition="'$(XUnitClassName)' != ''">$(AdditionalXHarnessArguments) -- -c=$(XUnitClassName)</AdditionalXHarnessArguments>
  </PropertyGroup>

  <ItemGroup Condition="'$(XUnitUseRandomizedTestOrderer)' == 'true'">
    <Compile Include="$(RepoRoot)src\libraries\Common\tests\Tests\RandomizedTestOrderAssemblyInfo.cs" />
  </ItemGroup>

  <Import Project="$(MSBuildThisFileDirectory)tests.android.targets"
          Condition="'$(TargetOS)' == 'android'" />
  <Import Project="$(MSBuildThisFileDirectory)tests.ioslike.targets"
          Condition="'$(TargetsAppleMobile)' == 'true'" />
  <Import Project="$(MSBuildThisFileDirectory)tests.browser.targets"
          Condition="'$(TargetOS)' == 'browser'" />
  <Import Project="$(MSBuildThisFileDirectory)tests.wasi.targets"
          Condition="'$(TargetOS)' == 'wasi'" />

  <PropertyGroup Condition="'$(RunAOTCompilation)' == 'true'">
    <_MobileIntermediateOutputPath>$(IntermediateOutputPath)mobile</_MobileIntermediateOutputPath>
  </PropertyGroup>

  <!-- This .targets file is also imported by the runtime Trimming tests, and we want to be able to manually configure trimming in them so this
  should be considered if we ever want to change the Condition of the ConfigureTrimming target -->
  <Target Name="ConfigureTrimming" Condition="('$(EnableAggressiveTrimming)' == 'true' or '$(EnableSoftTrimming)' == 'true') And '$(SkipConfigureTrimming)' != 'true'" AfterTargets="AddTestRunnersToPublishedFiles">
    <PropertyGroup>
      <TrimMode Condition="'$(EnableAggressiveTrimming)' == 'true'">link</TrimMode>
      <TrimMode Condition="'$(EnableSoftTrimming)' == 'true'">copyused</TrimMode>
    </PropertyGroup>

    <ItemGroup>
      <!-- Mark all the assemblies for link. We will explicitly mark the non-trimmable ones -->
      <ResolvedFileToPublish TrimMode="$(TrimMode)" />

      <!-- Don't trim the main assembly.
           TrimMode="" is needed so the root assemblies are correctly identified -->
      <ResolvedFileToPublish TrimMode="" Condition="'%(FileName)' == '$(AssemblyName)'" />

      <!-- TODO: find out why these assemblies aren't copied by copyused even though they're referenced -->
      <ResolvedFileToPublish TrimMode="Copy" Condition="'$(EnableSoftTrimming)' == 'true' and '%(FileName)' == 'System.ComponentModel.EventBasedAsync'" />
      <ResolvedFileToPublish TrimMode="Copy" Condition="'$(EnableSoftTrimming)' == 'true' and '%(FileName)' == 'System.Diagnostics.FileVersionInfo'" />
      <ResolvedFileToPublish TrimMode="Copy" Condition="'$(EnableSoftTrimming)' == 'true' and '%(FileName)' == 'System.Drawing.Primitives'" />
      <ResolvedFileToPublish TrimMode="Copy" Condition="'$(EnableSoftTrimming)' == 'true' and '%(FileName)' == 'System.IO.Pipelines'" />
      <ResolvedFileToPublish TrimMode="Copy" Condition="'$(EnableSoftTrimming)' == 'true' and '%(FileName)' == 'System.Memory'" />
      <ResolvedFileToPublish TrimMode="Copy" Condition="'$(EnableSoftTrimming)' == 'true' and '%(FileName)' == 'System.Net.WebHeaderCollection'" />
      <ResolvedFileToPublish TrimMode="Copy" Condition="'$(EnableSoftTrimming)' == 'true' and '%(FileName)' == 'System.Runtime.Serialization.Formatters'" />
      <ResolvedFileToPublish TrimMode="Copy" Condition="'$(EnableSoftTrimming)' == 'true' and '%(FileName)' == 'System.Security.AccessControl'" />
      <ResolvedFileToPublish TrimMode="Copy" Condition="'$(EnableSoftTrimming)' == 'true' and '%(FileName)' == 'System.Security.Claims'" />
      <ResolvedFileToPublish TrimMode="Copy" Condition="'$(EnableSoftTrimming)' == 'true' and '%(FileName)' == 'System.Security.Permissions'" />
      <ResolvedFileToPublish TrimMode="Copy" Condition="'$(EnableSoftTrimming)' == 'true' and '%(FileName)' == 'System.Transactions.Local'" />

      <!-- Even though we are trimming the test runner assembly, we want it to be treated
           as a root -->
      <TrimmerRootAssembly
          Condition="$([System.String]::Copy('%(ResolvedFileToPublish.FileName)%(ResolvedFileToPublish.Extension)').EndsWith('TestRunner.dll'))"
          Include="%(ResolvedFileToPublish.FullPath)" />
    </ItemGroup>

    <ItemGroup>
      <TrimmerRootDescriptor Include="$(MSBuildThisFileDirectory)ILLink.Descriptor.xunit.xml" Condition="'$(SkipXunitTrimmerDescriptor)' != 'true'"  />
      <TrimmerRootDescriptor Include="$(MSBuildThisFileDirectory)ILLink.Descriptor.TestUtilities.xml" Condition="'$(SkipTestUtilitiesReference)' != 'true'" />
    </ItemGroup>
  </Target>

  <Target Name="AddTestRunnersToPublishedFiles"
          AfterTargets="ComputeResolvedFilesToPublishList"
          Condition="'$(TestFramework)' == 'xunit'">
    <ItemGroup>
      <_runnerFilesToPublish Include="$(AndroidTestRunnerDir)*" Condition="'$(TargetOS)' == 'android'" />
      <_runnerFilesToPublish Include="$(AppleTestRunnerDir)*" Condition="'$(TargetOS)' == 'maccatalyst' or '$(TargetOS)' == 'ios' or '$(TargetOS)' == 'iossimulator' or '$(TargetOS)' == 'tvos' or '$(TargetOS)' == 'tvossimulator'" />
      <_runnerFilesToPublish Include="$(WasmTestRunnerDir)*" Condition="'$(TargetOS)' == 'Browser' or '$(TargetOS)' == 'wasi'" />

      <!-- Remove runner files that already exist in ResolvedFileToPublish to avoid double publishing -->
      <_resolvedFilesToPublishToFileName Include="@(ResolvedFileToPublish -> '%(FileName)%(Extension)')" />

      <_runnerFilesToPublishToFileName Include="@(_runnerFilesToPublish -> '%(FileName)%(Extension)')">
        <OriginalIdentity>%(Identity)</OriginalIdentity>
      </_runnerFilesToPublishToFileName>
      <_runnerFilesToPublishToFileName Remove="@(_resolvedFilesToPublishToFileName)" />

      <ResolvedFileToPublish Include="@(_runnerFilesToPublishToFileName -> '%(OriginalIdentity)')" RelativePath="%(FileName)%(Extension)" CopyToPublishDirectory="PreserveNewest" PostprocessAssembly="true" />
    </ItemGroup>
  </Target>

  <Import Project="$(MSBuildThisFileDirectory)workloads-testing.targets" Condition="'$(SkipWorkloadsTestingTargetsImport)' == 'true'" />

  <Target Name="PublishTestAsSelfContained"
          Condition="'$(IsCrossTargetingBuild)' != 'true'"
          AfterTargets="$(PublishTestAsSelfContainedAfterTargets)"
          DependsOnTargets="$(PublishTestAsSelfContainedDependsOn);$(BundleTestAppTargets);ArchiveTests" />

  <Target Name="PrepareForTestUsingWorkloads"
          BeforeTargets="Test"
          DependsOnTargets="InstallWorkloadUsingArtifacts"
          Condition="'$(InstallWorkloadForTesting)' == 'true'" />

</Project>
